package com.fzy.modules.goods.web;

import com.fzy.core.common.ApiController;
import com.fzy.core.common.ResponseData;
import com.fzy.modules.bill.service.PoBillDetailService;
import com.fzy.modules.bill.service.SoBillDetailService;
import com.fzy.modules.goods.entity.Goods;
import com.fzy.modules.goods.entity.GoodsMore;
import com.fzy.modules.goods.service.GoodsBomService;
import com.fzy.modules.goods.service.GoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品Controller
 */
@RestController
@RequestMapping(value = "/api/goods/good")
public class GoodsController extends ApiController {
    @Autowired
    GoodsService goodsService;
    @Autowired
    PoBillDetailService poBillDetailService;
    @Autowired
    SoBillDetailService soBillDetailService;
    @Autowired
    GoodsBomService goodsBomService;

    @PostMapping(value = "/list")
    public ResponseEntity<ResponseData> list(GoodsMore goods,
        @RequestParam(value = "page", defaultValue = "1") int page,
        @RequestParam(value = "rows", defaultValue = "10") int rows) {
        return new ResponseEntity<>(success(goodsService.findGoods(goods,page, rows)), HttpStatus.OK);
    }
    @RequestMapping(value = "/goodList")
    @ResponseBody
    public List<Goods> goodList(Goods goods) {
        return goodsService.goodList(goods);
    }

    @PostMapping(value = "")
    public ResponseEntity<ResponseData> save(Goods goods,String picAddr) {
    	
        if (StringUtils.isNotEmpty(goods.getId())) {
            if(goodsService.isOne(goods).size()>0){
                return new ResponseEntity<>(fail("该编号已存在商品"),HttpStatus.BAD_GATEWAY);
            }
            return new ResponseEntity<>(success(goodsService.update(goods)), HttpStatus.OK);
        }
        //TODO:添加商品图片表的信息
        Goods goods1=new Goods();
        goods1.setCode(goods.getCode());//检测编号是否重复
        if(goodsService.goodList(goods1).size()>0){
            return new ResponseEntity<>(fail("该编号已存在商品"),HttpStatus.BAD_GATEWAY);
        }
        
        //调用商品添加方法
        try {
			goodsService.saveGoods(goods,picAddr);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseEntity<>(fail("添加失败"), HttpStatus.BAD_GATEWAY);
		}
        return new ResponseEntity<>(success("添加成功"), HttpStatus.OK);
    }

    @DeleteMapping(value = "/{id}")
    public ResponseEntity<ResponseData> delete(@PathVariable String id) {
        if (poBillDetailService.isWithPoBillDetail(id) > 0) {
            return new ResponseEntity<>(fail("该商品在采购订单中被使用，不能删除"), HttpStatus.BAD_GATEWAY);
        }
        if (soBillDetailService.isWithSoBillDetail(id) > 0) {
            return new ResponseEntity<>(fail("该商品在销售订单中被使用，不能删除"), HttpStatus.BAD_GATEWAY);
        }
        if (goodsBomService.findAllList(id).size() > 0) {
            return new ResponseEntity<>(fail("该商品存在子商品，不能删除"), HttpStatus.BAD_GATEWAY);
        }
        return new ResponseEntity<>(success(goodsService.delete(id)), HttpStatus.OK);
    }
    @RequestMapping("/goodsJsonList")
    @ResponseBody
   public List<Goods> goodsJsonList(Goods goods){
    	return goodsService.goodsList(goods);
   } 
    @PostMapping(value = "/listGoodsMore")
    public ResponseEntity<ResponseData> listGoodsMore(GoodsMore goodsMore){
        return new ResponseEntity<>(success(goodsService.findGoodsMore(goodsMore)), HttpStatus.OK);
    }
    /**
     * 通过GoodsIds批量查询商品信息
     * 注意：goodsId数据格式因为是字符串所以格式如：'151748086502949','097A7CBD-DE41-11E7-8251-005056B94044'才合法
     * @param goodsIds
     * @return
     */
    @GetMapping("/listByGoodsIds")
    public ResponseEntity<ResponseData> listBySoBillIds(String goodsIds){
    	if(goodsIds==null||"".equals(goodsIds.trim())){
    		return new ResponseEntity<>(fail("参数不正确"), HttpStatus.BAD_GATEWAY);
    	}
    	if(goodsIds.endsWith(",")){
    		goodsIds=goodsIds.substring(0, goodsIds.lastIndexOf(","));
    	}
    	String[] goodsIdStrs = goodsIds.split(",");
    	for(String goodsIdStr:goodsIdStrs){
    		if(!goodsIdStr.startsWith("'")||!goodsIdStr.endsWith("'")){
    			return new ResponseEntity<>(fail("参数格式不合法"), HttpStatus.BAD_GATEWAY);
    		}
    	}
        return new ResponseEntity<>(success(goodsService.listBySoBillIds(goodsIds)), HttpStatus.OK);
    }
}
